package com.liqi.common.utils;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * mybatis plus 提供的代码生成器
 * 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码
 *
 * @link https://mp.baomidou.com/guide/generator.html
 */
public class CodeGenerator {

    private final static String model = "device";

    // 数据库 URL
    private static final String URL = "jdbc:mysql://121.33.195.139:53306/device_back?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
    // 数据库驱动
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    // 数据库用户名
    private static final String USERNAME = "device_back";
    // 数据库密码
    private static final String PASSWORD = "Liqi@123456";
    // @author 值
    private static final String AUTHOR = "tianye";
    // 包的基础路径
    private static final String BASE_PACKAGE_URL = "com.liqi";
    // xml文件路径
    private static final String XML_PACKAGE_URL = "/src/main/resources/mapper/" + model + "/";
    // controller,service文件路径
    private static final String CONTROLLER_SERVICE_PACKAGE_URL = "/src/main/java/com/liqi/web/";
    // xml 文件模板
    private static final String XML_MAPPER_TEMPLATE_PATH = "/templates/mapper.xml.ftl";
    // controller 文件模板
    private static final String CONTROLLER_TEMPLATE_PATH = "/templates/controller.java.ftl";
    // serviceImpl 文件模板
    private static final String SERVICE_IMPL_TEMPLATE_PATH = "/templates/serviceImpl.java.ftl";


    public static void main(String[] args) {
//        String tables = "t_cfg_branch,t_cfg_subject,t_rel_service_branch,t_rel_service_subject";
        String tables = "dv_plugin_version";
        String[] tableArray = tables.split(",");
        CodeGenerator.codeGenerator(tableArray);
    }

    public static void codeGenerator(String[] tables) {
        AutoGenerator generator = new AutoGenerator();

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);
        globalConfig.setOutputDir(projectPath + "/device-system" + "/src/main/java");
        System.out.println(globalConfig.getOutputDir());
//        int i = 1 / 0;
        globalConfig.setAuthor(AUTHOR);
        globalConfig.setSwagger2(true);
        globalConfig.setOpen(false);
        // 是否覆盖已有文件
        globalConfig.setFileOverride(true);
        globalConfig.setServiceImplName("%sService");
        globalConfig.setServiceName(null);
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setBaseColumnList(true);
        globalConfig.setBaseResultMap(true);
        generator.setGlobalConfig(globalConfig);

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(URL);
        dataSourceConfig.setDriverName(DRIVER_NAME);
        dataSourceConfig.setUsername(USERNAME);
        dataSourceConfig.setPassword(PASSWORD);
        generator.setDataSource(dataSourceConfig);

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(BASE_PACKAGE_URL);
        packageConfig.setController("web.controller." + model);
        packageConfig.setServiceImpl("web.service." + model);
        packageConfig.setMapper("dev.mapper");
        packageConfig.setEntity("dev.domain");
        packageConfig.setModuleName(null);
        generator.setPackageInfo(packageConfig);
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // controller
        focList.add(new FileOutConfig(CONTROLLER_TEMPLATE_PATH) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/device-admin" + CONTROLLER_SERVICE_PACKAGE_URL + "/controller/" + model
                        + "/" + tableInfo.getEntityName() + "Controller" + StringPool.DOT_JAVA;
            }
        });
        // service
        focList.add(new FileOutConfig(SERVICE_IMPL_TEMPLATE_PATH) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/device-admin" + CONTROLLER_SERVICE_PACKAGE_URL + "/service/" + model
                        + "/" + tableInfo.getEntityName() + "Service" + StringPool.DOT_JAVA;
            }
        });
        // mapper-xml文件
        focList.add(new FileOutConfig(XML_MAPPER_TEMPLATE_PATH) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/device-system" + XML_PACKAGE_URL
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        generator.setCfg(cfg);

        // 配置自定义代码模板
        TemplateConfig templateConfig = new TemplateConfig();
        // 设置为null，表示不生成service接口
        templateConfig.setService(null);
        templateConfig.setXml(null);
        templateConfig.setServiceImpl(null);
        templateConfig.setController(null);
        generator.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);

        // 表名
        strategy.setInclude(tables);
        strategy.setControllerMappingHyphenStyle(false);
        // 去掉表名的前缀
//        strategy.setTablePrefix("t_");
        generator.setStrategy(strategy);
        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        generator.execute();
    }
}
